package zucc.lcj.android_finalwork.me;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import zucc.lcj.android_finalwork.FriendsActivity;
import zucc.lcj.android_finalwork.help.HelpActivity;
import zucc.lcj.android_finalwork.LoginActivity;
import zucc.lcj.android_finalwork.MyDatabaseHelper;
import zucc.lcj.android_finalwork.R;
import zucc.lcj.android_finalwork.stars.StarsActivity;
import zucc.lcj.android_finalwork.guide.GuideActivity;


public class MeActivity extends AppCompatActivity {
    private static final String TAG = "test";
    Button more3;
    Button more4;
    Button more5;
    Button more2;
    Button more1;
    ImageView header;
    TextView user_name,integrals,sends,helps,haves;
    ImageView medal1,medal2,medal3,medal4,medal5,medal6,medal7,medal8;
    private MyDatabaseHelper dbHelper;
    private  int integral_number,send_number,help_number,have_number;
    private String headerUrl;

    protected static final int CHANGE_UI = 1;
    protected static final int ERROR = 2;

    // 主线程创建消息处理器
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == CHANGE_UI) {
                Bitmap bitmap = (Bitmap) msg.obj;
                header.setImageBitmap(bitmap);
            } else if (msg.what == ERROR) {
                Toast.makeText(MeActivity.this, "显示图片错误",
                        Toast.LENGTH_SHORT).show();
            }
        }
    };


    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me);
        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);
        header = (ImageView) findViewById(R.id.user) ;

        more1 = (Button) findViewById(R.id.me_more1) ;
        more1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeActivity.this, MyHelpActivity.class);
                startActivity(intent);
            }
        });


        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);
        header = (ImageView) findViewById(R.id.user) ;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursorImg = db.rawQuery("select * from UserInfo where user_name=?",new String[]{LoginActivity.global_login_user});
        if (cursorImg.moveToFirst()){
            headerUrl=cursorImg.getString(cursorImg.getColumnIndex("user_header"));
        }
        if (headerUrl!= null) {
            //子线程请求网络,Android4.0以后访问网络不能放在主线程中
            if (headerUrl.equals("https://bkimg.cdn.bcebos.com/pic/63d0f703918fa0ec08faacc53fde4eee3d6d54fb6deb?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxODA=,g_7,xp_5,yp_5/format,f_auto")){
                new Thread() {
                    private HttpURLConnection conn;
                    private Bitmap bitmap;

                    public void run() {
                        // 连接服务器 get 请求 获取图片
                        try {
                            //创建URL对象
                            URL url = new URL(headerUrl);
                            // 根据url 发送 http的请求
                            conn = (HttpURLConnection) url.openConnection();
                            // 设置请求的方式
                            conn.setRequestMethod("GET");
                            //设置超时时间
                            conn.setConnectTimeout(5000);
                            // 得到服务器返回的响应码
                            int code = conn.getResponseCode();
                            //请求网络成功后返回码是200
                            if (code == 200) {
                                //获取输入流
                                InputStream is = conn.getInputStream();
                                //将流转换成Bitmap对象
                                bitmap = BitmapFactory.decodeStream(is);
                                //将更改主界面的消息发送给主线程
                                Message msg = new Message();
                                msg.what = CHANGE_UI;
                                msg.obj = bitmap;
                                handler.sendMessage(msg);
                            } else {
                                //返回码不等于200 请求服务器失败
                                Message msg = new Message();
                                msg.what = ERROR;
                                handler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = new Message();
                            msg.what = ERROR;
                            handler.sendMessage(msg);
                        }
                        //关闭连接
                        conn.disconnect();
                    }
                }.start();
            }
            else {
                Bitmap bitmap = BitmapFactory.decodeFile(headerUrl);
                header.setImageBitmap(bitmap);
            }

        }



        user_name = (TextView) findViewById(R.id.user_name);
        user_name.setText(LoginActivity.global_login_user.toString());
        integrals = (TextView) findViewById(R.id.integrals);
        sends = (TextView) findViewById(R.id.sends);
        helps = (TextView) findViewById(R.id.helps);
        haves = (TextView) findViewById(R.id.haves);

        String name = LoginActivity.global_login_user;
        Cursor cursor = db.rawQuery("select * from UserInfo where user_name=?",new String[]{name});
        if(cursor.moveToNext()) {
            integral_number=cursor.getInt(cursor.getColumnIndex("integral"));
            integrals.setText(String.valueOf(integral_number));
            send_number=cursor.getInt(cursor.getColumnIndex("helpPost"));
            sends.setText(String.valueOf(send_number));
            help_number=cursor.getInt(cursor.getColumnIndex("helpNum"));
            helps.setText(String.valueOf(help_number));
            have_number=cursor.getInt(cursor.getColumnIndex("medal"));
            haves.setText(String.valueOf(have_number));
        }
//        点亮勋章
        medal1 = (ImageView) findViewById(R.id.medal_1);
        medal2 = (ImageView) findViewById(R.id.medal_2);
        medal3 = (ImageView) findViewById(R.id.medal_3);
        medal4 = (ImageView) findViewById(R.id.medal_4);
        medal5 = (ImageView) findViewById(R.id.medal_5);
        medal6 = (ImageView) findViewById(R.id.medal_6);
        medal7 = (ImageView) findViewById(R.id.medal_7);
        medal8 = (ImageView) findViewById(R.id.medal_8);
        if(help_number>=1){
            medal1.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=1 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=5){
            medal2.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=2 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=10){
            medal3.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=3 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=20){
            medal4.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=4 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=50){
            medal5.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=5 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=100){
            medal6.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=6 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=150){
            medal7.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=7 where UserInfo.user_name=?",new String[]{name});
        }
        if(help_number>=200){
            medal8.setImageResource(R.drawable.me_medal_get);
            db.execSQL("update UserInfo set medal=8 where UserInfo.user_name=?",new String[]{name});
        }

        Cursor cursor1 = db.rawQuery("select * from UserInfo where user_name=?",new String[]{name});
        if(cursor1.moveToNext()) {
            have_number=cursor1.getInt(cursor1.getColumnIndex("medal"));
        }
        haves = (TextView) findViewById(R.id.haves);
        haves.setText(String.valueOf(have_number));
        System.out.println("number "+have_number);


//        更多
        more2 = (Button) findViewById(R.id.me_more2);
        more2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String name = LoginActivity.global_login_user;
                AlertDialog.Builder builder = new AlertDialog.Builder(MeActivity.this);
                builder.setTitle("从池中拿出微信");
                builder.setMessage("您确定要拿出微信吗？这样就没有小姐姐(小哥哥)可以拿到您的微信了，那您离脱单又远了一步！");
                builder.setPositiveButton("确定拿出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String sex="";
                        Cursor cursor = db.rawQuery("select * from UserInfo where UserInfo.user_name=?",new String[]{name});
                        if (cursor.moveToFirst()){
                            sex=cursor.getString(cursor.getColumnIndex("user_sex"));
                        }

                        db.execSQL("update UserInfo set isWechat=0 where UserInfo.user_name=?",new String[]{name});
                        if (sex.equals("男")){
                            db.execSQL("delete from WechatMen where wechatMen_user=?",new Integer[]{LoginActivity.global_login_user_id});
                        }

                        else if (sex.equals("女")){
                            db.execSQL("delete from WechatWomen where wechatWomen_user=?",new Integer[]{LoginActivity.global_login_user_id});
                        }
                        Toast.makeText(MeActivity.this, "成功拿出",Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNeutralButton("再考虑一下", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MeActivity.this,"一定要慎重考虑呀！",Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });


        ImageView bar_1 = (ImageView) findViewById(R.id.bar_1);
        ImageView bar_2 = (ImageView) findViewById(R.id.bar_2);
        ImageView bar_3 = (ImageView) findViewById(R.id.bar_3);
        ImageView bar_4 = (ImageView) findViewById(R.id.bar_4);
        ImageView bar_5 = (ImageView) findViewById(R.id.bar_5);
        bar_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeActivity.this, FriendsActivity.class);
                startActivity(intent);
            }
        });
        bar_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });
        bar_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeActivity.this, GuideActivity.class);
                startActivity(intent);
            }
        });
        bar_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeActivity.this,StarsActivity.class);
                startActivity(intent);
            }
        });






//        跳转到城院汇规范
        more3=findViewById(R.id.me_more3);
        more3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MeActivity.this, StandardActivity.class);
                startActivity(intent);
            }
        });
//        跳转到设置
        more4=findViewById(R.id.me_more4);
        more4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MeActivity.this, SetupActivity.class);
                startActivity(intent);
            }
        });
//        跳转到关于我们
        more5=findViewById(R.id.me_more5);
        more5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MeActivity.this, AboutMeActivity.class);
                startActivity(intent);
            }
        });
    }
}